package com.liesheng.haylou.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;

@SuppressLint("SimpleDateFormat")
public class TimeUtils {
    public static final SimpleDateFormat DATE_FORMAT_DATE;

    public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    static {
        DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");
    }

    public static long getCurrentTimeInLong() {
        return System.currentTimeMillis();
    }

    public static String getCurrentTimeInString() {
        return getTime(getCurrentTimeInLong());
    }

    public static String getCurrentTimeInString(SimpleDateFormat simpleDateFormat) {
        return getTime(getCurrentTimeInLong(), simpleDateFormat);
    }

    public static String getTime(long timeMillis) {
        return getTime(timeMillis, DEFAULT_DATE_FORMAT);
    }

    /**
     * 长整型数字转日期, 返回字符串形式的日期
     *
     * @param date
     */
    public static String getTime(long timeMillis, SimpleDateFormat simpleDateFormat) {
        return simpleDateFormat.format(new Date(timeMillis));
    }

    public static Date stringToDate(String date) {
        Date localObject = null;
        try {
            localObject = DATE_FORMAT_DATE.parse(date);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return localObject;
    }

    public static String stringToDateDetail(Date date) {
        String localObject = "";
        try {
            localObject = DATE_FORMAT_DATE.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return localObject;
    }

    public static Date stringToDateTime(String date) {
        Date localObject = null;
        try {
            localObject = DEFAULT_DATE_FORMAT.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return localObject;
    }

    /***
     * 将String格式的日期转化成long类型的
     *
     * @param sourceTime
     * @return
     */
    public static long string2LongTime(String sourceTime) {
        if (!StringUtils.isEmpty(sourceTime))
            return stringToDate(sourceTime).getTime();
        return 0;
    }

    /***
     * 将String格式的日期转化成long类型的
     *
     * @param sourceTime
     * @return
     */
    public static long string2LongTime2(String sourceTime) {
        if (!StringUtils.isEmpty(sourceTime))
            return stringToDateTime(sourceTime).getTime();
        return 0;
    }

    /**
     * @param mss 要转换的毫秒数
     * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
     */
    public static String formatDuring(long mss) {
        long days = mss / (1000 * 60 * 60 * 24);
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;
        StringBuilder sb = new StringBuilder("");
        if (days > 0) {
            sb.append(days).append("天");
        }
        if (hours > 0) {
            sb.append(hours).append("小时");
        }
        if (minutes > 0) {
            sb.append(minutes).append("分");
        }
        sb.append(seconds).append("秒");
        return sb.toString();
    }

    public static String getFormatTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static Calendar getFormatTime(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(format.parse(date));
            return calendar;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}